<?php


namespace app\controller;

use app\service\GoodsService;
use app\utils\Datetime;
use think\facade\Request;

/**
 * 商品
 */
class GoodsController
{
    /**
     * 根据商品名称搜索类似商品
     */
    public function queryGoodsByGoods() {
        $param['goods'] = Request::param('goods');
        $param['type'] = Request::param('type');
        $param['startPage'] = Request::param('startPage');
        $param['endPage'] = Request::param('endPage');
        return json(GoodsService::queryGoodsByGoods($param));
    }

    /**
     * 根据商品uuid获取商品详情
     */
    public function queryGoodsByUuid() {
        $param['goods_uuid'] = Request::param('goods_uuid');
        $param['user_uuid'] = Request::param('user_uuid');
        return json(GoodsService::queryGoodsByUuid($param));
    }

    /**
     * 新增商品
     */
    public function createGoods() {
        $param['goods'] = Request::param('goods');
        $param['goods_explain'] = Request::param('goods_explain');
        $param['price'] = Request::param('price');
        $param['goods_num'] = Request::param('goods_num');
        $param['address'] = Request::param('address');
        $param['img'] = Request::param('img');
        $param['son_category'] = Request::param('son_category');
        $param['father_category'] = Request::param('father_category');
        $param['tag'] = Request::param('tag');
        $param['discount'] = Request::param('discount');
        $param['discount_start_time'] = Request::param('discount_start_time');
        $param['discount_end_time'] = Request::param('discount_end_time');
        $param['goods_level'] = Request::param('goods_level');
        $param['parameter'] = Request::param('parameter');
        $param['create_time'] = Datetime::today();
        $param['pay_num'] = 0;
        $param['evaluate_num'] = 0;
        $param['love_num'] = 0;
        $param['status'] = 1;

        $chars = md5(uniqid(mt_rand(), true));
        $param['uuid'] = substr ( $chars, 0, 8 ) . '-'
            . substr ( $chars, 8, 4 ) . '-'
            . substr ( $chars, 12, 4 ) . '-'
            . substr ( $chars, 16, 4 ) . '-'
            . substr ( $chars, 20, 12 );

        return json(GoodsService::createGoods($param));
    }

    /**
     * 更新商品信息
     */
    public function updateGoods() {
        $param['uuid'] = Request::param('uuid');

        $param['goods'] = Request::param('goods');
        $param['goods_explain'] = Request::param('goods_explain');
        $param['price'] = Request::param('price');
        $param['goods_num'] = Request::param('goods_num');
        $param['address'] = Request::param('address');
        $param['img'] = Request::param('img');
        $param['son_category'] = Request::param('son_category');
        $param['father_category'] = Request::param('father_category');
        $param['tag'] = Request::param('tag');
        $param['discount'] = Request::param('discount');
        $param['discount_start_time'] = Request::param('discount_start_time');
        $param['discount_end_time'] = Request::param('discount_end_time');
        $param['status'] = Request::param('status');
        $param['goods_level'] = Request::param('goods_level');
        $param['parameter'] = Request::param('parameter');

        return json(GoodsService::updateGoods($param));
    }

    /**
     * 根据收藏数返回商品列表
     */
    public function listGoodsByLoveNum() {
        $param['startPage'] = Request::param('startPage');
        $param['endPage'] = Request::param('endPage');
        return json(GoodsService::listGoodsByLoveNum($param));
    }

    /**
     * 根据付款数返回商品列表
     */
    public function listGoodsByPayNum() {
        $param['startPage'] = Request::param('startPage');
        $param['endPage'] = Request::param('endPage');
        return json(GoodsService::listGoodsByPayNum($param));
    }

    /**
     * 根据折扣力度返回打折商品列表
     */
    public function listGoodsByDiscount() {
        $param['startPage'] = Request::param('startPage');
        $param['endPage'] = Request::param('endPage');
        return json(GoodsService::listGoodsByDiscount($param));
    }

    /**
     * 根据商品级别返回商品列表
     */
    public function listGoodsByLevel() {
        $param['goods_level'] = Request::param('goods_level');
        $param['startPage'] = Request::param('startPage');
        $param['endPage'] = Request::param('endPage');
        return json(GoodsService::listGoodsByLevel($param));
    }

    /**
     * 根据父类别返回商品列表
     */
    public function listGoodsByFatherCategory() {
        $param['son_category'] = Request::param('father_category');
        $param['startPage'] = Request::param('startPage');
        $param['endPage'] = Request::param('endPage');
        return json(GoodsService::listGoodsByFatherCategory($param));
    }

    /**
     * 根据子类别返回商品列表
     */
    public function listGoodsBySonCategory() {
        $param['son_category'] = Request::param('son_category');
        $param['startPage'] = Request::param('startPage');
        $param['endPage'] = Request::param('endPage');
        return json(GoodsService::listGoodsBySonCategory($param));
    }
}
